import DateUtil from "../engine/DateUtil";
import { Logger } from "../engine/Logger";
import { ScreenShotHelper } from "../engine/ScreenShotHelper";
import UIManager from "../engine/UIManager";
import { UIHelper } from "../game/util/UIHelper";
import { HttpHelper } from "../net/HttpHelper";
import { NativeSdkHelper } from "./NativeSdkHelper";

const Native_Android_Class: string = "com/jhaosdk/WechatSdkManager"
const Native_IOS_Class: string = "WechatSdkManager"

export class WechatSdkManager {

    public static instance: WechatSdkManager = new WechatSdkManager();

    public static init() {
        window["WechatSdkManager"] = WechatSdkManager;
    }

    public login() {
        let state: string = "jhao-" + DateUtil.now()
        if (NativeSdkHelper.isAndroid()) {
            jsb.reflection.callStaticMethod(Native_Android_Class, "login", "(Ljava/lang/String;)V", state);
        }
        else if (NativeSdkHelper.isIos()) {
            jsb.reflection.callStaticMethod(Native_IOS_Class, "login:", state);
        }
        else {
            UIManager.instance.hideLoadingIcon()
            UIHelper.showTip("该平台暂不支持微信登录")
        }
    }

    // {"openid":"oGtMy6tM6-7xJoRQAdjAUYinrwZ8","nickname":"jhao","sex":0,"language":"zh_CN","city":"","province":"","country":"","headimgurl":"https://thirdwx.qlogo.cn/mmopen/vi_32/wvTibQFXYnMlXGYx0VmJgpJxP7micf9ZQicuVhKS7AkWtmicMicBMxH0QfxAviaflUE5ibI9ye1wIImTmcBNgWpNI51kA/132","privilege":[],"unionid":"ovMVv6iBRmquF3xvh-e8SK6SHZB0"}
    public static login_callback(code: string) {
        Logger.info("login_callback==", code)
        HttpHelper.wechat_auth(code)
    }


    // * @param scene @param scene  0:分享到对话: 1:分享到朋友圈: 2:分享到收藏:
    public static share_url(title: string, content: string, scene: number = 0, url: string = "http://baidu.com") {
        let params: object = {
            "title": title,
            "description": content,
            "scene": scene,
            "url": url,
        }
        if (NativeSdkHelper.isAndroid()) {
            jsb.reflection.callStaticMethod(Native_Android_Class, "share_url", "(Ljava/lang/String;)V", JSON.stringify(params));
        }
        else if (NativeSdkHelper.isIos()) {
            jsb.reflection.callStaticMethod(Native_IOS_Class, "share_url:", JSON.stringify(params));
        }
        else {
            UIHelper.showTip("该平台暂时不支持分享")
        }
    }

    public static captureScreenAndShare(scene:number) {
        let fileName: string = "share.jpg";
        if (cc.sys.isNative) {
            let fullPath: string = jsb.fileUtils.getWritablePath() + fileName;
            if (jsb.fileUtils.isFileExist(fullPath)) {
                jsb.fileUtils.removeFile(fullPath);
            }
            ScreenShotHelper.captureScreen(fullPath)
            this.share_img(fullPath, scene)
        } else {
            // let base64tr = ScreenShotHelper.capture("")
        }
    }

    public static captureScreenAndSaveToAlbum(){
        let fileName: string = "share.jpg";
        if (cc.sys.isNative) {
            let fullPath: string = jsb.fileUtils.getWritablePath() + fileName;
            if (jsb.fileUtils.isFileExist(fullPath)) {
                jsb.fileUtils.removeFile(fullPath);
            }
            ScreenShotHelper.captureScreen(fullPath)

            if (NativeSdkHelper.isAndroid()) {
                jsb.reflection.callStaticMethod("com/jhaosdk/NativeSdkHelper", "saveToNativeAlbum", "(Ljava/lang/String;)V", fullPath);
                UIHelper.showTip("保存到相册成功")
            }
            else if (NativeSdkHelper.isIos()) {
                jsb.reflection.callStaticMethod("NativeSdkHelper", "setFileToLocal:",fullPath));
                UIHelper.showTip("保存到相册成功")
            }
            else {
                UIHelper.showTip("该平台暂时不支持分享")
            }
            // this.share_img(fullPath, scene)
        } else {
            // let base64tr = ScreenShotHelper.capture("")
        }
    }

    public static captureNodeAndShare(scene: number, node: cc.Node) {
        if (cc.sys.isNative) {
            let picData: Uint8Array = ScreenShotHelper.captureNode(node)
            let picPath: string = jsb.fileUtils.getWritablePath() + "share.png"
            if (jsb.fileUtils.isFileExist(picPath)) {
                jsb.fileUtils.removeFile(picPath);
            };
            ScreenShotHelper.saveDataToFile(picData, node.width, node.height, picPath)
            this.share_img(picPath, scene)
        }
    }

    // @param scene @param scene  0:分享到对话: 1:分享到朋友圈: 2:分享到收藏:
    public static share_img(picPath: string, scene: number = 0) {
        let params: object = {
            "scene": scene,
            "picPath": picPath,
        }
        if (NativeSdkHelper.isAndroid()) {
            jsb.reflection.callStaticMethod(Native_Android_Class, "share_img", "(Ljava/lang/String;)V", JSON.stringify(params));
        }
        else if (NativeSdkHelper.isIos()) {
            jsb.reflection.callStaticMethod(Native_IOS_Class, "share_img:", JSON.stringify(params));
        }
    }

    public static share_callback(param: string) {
        Logger.log("share_callback===", param)
    }
}